using System;
using System.Collections.Generic;
using OracleInternal.Common;

namespace OracleInternal.TTC.Accessors
{
	internal class TTCRefCursorAccessor : Accessor
	{
		internal List<TTCResultSet> m_TTCResultSetList;

		internal Accessor[] DefineAccessorForCurrentRow
		{
			set
			{
				m_TTCResultSetList[m_lastRowProcessed - 1].DefineAccessors = value;
			}
		}

		internal SQLMetaData SqlMetaDataForCurrentRow => m_TTCResultSetList[m_lastRowProcessed - 1].SqlMetaData;

		internal TTCRefCursorAccessor(ColumnDescribeInfo colMetaData, MarshallingEngine marshallingEngine)
			: base(colMetaData, marshallingEngine, bForBind: true)
		{
		}

		internal override void Initialize(ColumnDescribeInfo colMetaData, MarshallingEngine marshallingEngine, bool bForBind)
		{
			if (m_TTCResultSetList != null)
			{
				m_TTCResultSetList.Clear();
			}
			base.Initialize(colMetaData, marshallingEngine, bForBind);
		}

		internal override byte[] GetByteRepresentation(int currentRow)
		{
			throw new NotSupportedException();
		}

		internal override bool IsNullIndicatorSet(int currentRow)
		{
			bool result = true;
			if (m_TTCResultSetList[currentRow].CursorId != 0)
			{
				result = false;
			}
			return result;
		}

		internal TTCResultSet GetResultSet(int currentRow)
		{
			return m_TTCResultSetList[currentRow];
		}

		internal override bool UnmarshalOneRow()
		{
			if (!m_bNullByDescribe)
			{
				if (m_TTCResultSetList == null)
				{
					m_TTCResultSetList = new List<TTCResultSet>();
				}
				TTCResultSet tTCResultSet = new TTCResultSet();
				TTCDescribeInfo.ReadMessage(bForDescribe: false, bForRefCursor: true, m_marshallingEngine, tTCResultSet.SqlMetaData, bIgnoreMetadata: false);
				tTCResultSet.CursorId = (int)m_marshallingEngine.UnmarshalUB4();
				if (m_statementType == SqlStatementType.PLSQL)
				{
					m_marshallingEngine.UnmarshalSB2();
				}
				m_TTCResultSetList.Add(tTCResultSet);
				m_lastRowProcessed++;
			}
			return false;
		}
	}
}
